//
//  LXLoadingView.m
//  bgms_d
//
//  Created by lachesis-zhu on 03/07/2017.
//  Copyright © 2017 lachesis-zhu. All rights reserved.
//

#import "LXLoadingView.h"
#import "LXWaveWaterView.h"
#import "UIView+UserInteraction.h"
#import "UIView+LXExpanding.h"
#import "InfiniteRotationView.h"
#import "UIImage+ImageEffects.h"
#import "UIView+GlowView.h"

@interface LXLoadingView ()

@property (nonatomic, strong)  UIView  *blackView;
@property (nonatomic, strong)  UIView  *messageView;
@property (nonatomic, strong)  LXWaveWaterView *waterView;

@end

@implementation LXLoadingView

- (void)show {
    
    if (self.contentView) {
        [self.contentView addSubview:self];
        
        self.contentViewUserInteractionEnabled == NO ? [self.contentView enabledUserInteraction] : 0;
        
        [self createBlackView];
        [self createMessageView];
        
        if (self.autoHiden) {
            [self performSelector:@selector(hide) withObject:nil afterDelay:self.delayAutoHidenDuration];
        }
    }
}

- (void)hide {
    if (self.contentView) {
        [UIView animateWithDuration:0.5 animations:^{
            [self.waterView setProgress:1.0f];
        } completion:^(BOOL finished) {
            [self removeViews];
        }];
    }
}

- (void)removeViews {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(baseMessageViewWillDisappear:)]) {
        [self.delegate baseMessageViewWillDisappear:self];
    }
    
    [UIView animateWithDuration:0.3f animations:^{
        self.blackView.alpha   = 0.f;
        self.messageView.alpha = 0.f;
    } completion:^(BOOL finished) {
        self.contentViewUserInteractionEnabled == NO ? [self.contentView disableUserInteraction] : 0;
        [self removeFromSuperview];
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(baseMessageViewDidDisappear:)]) {
            [self.delegate baseMessageViewDidDisappear:self];
        }
    }];
}

- (void)createBlackView {
    
    self.blackView                 = [[UIView alloc] initWithFrame:self.contentView.bounds];
    self.blackView.backgroundColor = kAppLightBackgroundColor;
    self.blackView.alpha = 0.15f;
    [self addSubview:self.blackView];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(baseMessageViewWillAppear:)]) {
        [self.delegate baseMessageViewWillAppear:self];
    }
    
    [UIView animateWithDuration:0.3f animations:^{
        
    } completion:^(BOOL finished) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(baseMessageViewDidAppear:)]) {
            [self.delegate baseMessageViewDidAppear:self];
        }
    }];
}

- (void)createMessageView {
    
    // 创建信息窗体view
    self.messageView                     = [[UIView alloc] init];
    
    if (self.messageObject) {
        self.messageView.frame = CGRectMake(0, 0, 100, 110);
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, 100, 15)];
        titleLabel.font = [UIFont lx_systemFontOfSize:12];
        titleLabel.textColor = kAppNormalTextColor;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        MessageViewObject *message = (MessageViewObject *)self.messageObject;
        titleLabel.text = message.content;
        [self.messageView addSubview:titleLabel];
    } else {
        self.messageView.frame = CGRectMake(0, 0, 100, 100);
    }
    
    self.messageView.center              = self.contentView.lx_middlePoint;
    self.messageView.layer.cornerRadius  = self.messageView.lx_width / 2.f;
    self.messageView.layer.masksToBounds = YES;
    self.messageView.alpha               = 0.f;
    [self addSubview:self.messageView];
    
    {
        InfiniteRotationView *rotateView = [[InfiniteRotationView alloc] initWithFrame:self.messageView.bounds];
        rotateView.speed                 = 0.95f;
        rotateView.clockWise             = YES;
        [rotateView startRotateAnimation];
        [self.messageView addSubview:rotateView];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
        imageView.image        = [UIImage imageNamed:@"line"];
        imageView.center       = rotateView.lx_middlePoint;
        [rotateView addSubview:imageView];
    }
    
//    UIImageView *inImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"word"]];
//    inImageView.scale        = 0.3f;
//    [self.messageView addSubview:inImageView];
//    inImageView.center = self.messageView.lx_middlePoint;
//    
//    // Start glow.
//    inImageView.glowRadius            = @(2.f);
//    inImageView.glowOpacity           = @(1.f);
//    inImageView.glowColor             = [[UIColor colorWithRed:0.203  green:0.598  blue:0.859 alpha:1] colorWithAlphaComponent:0.95f];
//    inImageView.glowDuration          = @(1.f);
//    inImageView.hideDuration          = @(3.f);
//    inImageView.glowAnimationDuration = @(2.f);
//    [inImageView createGlowLayer];
//    [inImageView insertGlowLayer];
//    [inImageView startGlowLoop];
    
    CGRect rect = CGRectMake(self.messageView.frame.size.width / 8 * 3, self.messageView.frame.size.height / 8 * 3, self.messageView.frame.size.width / 4, self.messageView.frame.size.height / 4);
    self.waterView = [[LXWaveWaterView alloc] initWithFrame:rect];
    self.waterView.progress = .25;
    self.waterView.progress_animation = YES;
    self.waterView.changeFrame = rect;
    self.waterView.borderPath = [self waterDownPathRect:self.waterView.frame lineWidth:0];
    self.waterView.border_fillColor = [UIColor colorWithHexString:@"7d61e2"];
    [self.messageView addSubview:self.waterView];
    
    [UIView animateWithDuration:0.3f animations:^{
        self.messageView.alpha = 1.f;
    }];
}

- (void)stopLoading {
    [self hide];
}

- (UIBezierPath *)waterDownPathRect:(CGRect)rect lineWidth:(CGFloat)width {
    
    CGFloat radius = rect.size.width / 4;
    CGPoint center1 = CGPointMake(radius * 2, radius * 3);
    CGPoint top = CGPointMake(2 * radius, radius / 2);
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center1 radius:radius startAngle:0 endAngle:M_PI clockwise:YES];
    [path addLineToPoint:top];
    [path setLineCapStyle:kCGLineCapRound];
    [path setLineWidth:width];
    
    return path;
}
@end
